package net.qhzw.vip.qiyu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.qhzw.vip.qiyu.entity.GuaranteeUser;
import net.qhzw.vip.qiyu.handler.MyException;
import net.qhzw.vip.qiyu.model.BoxVo;
import net.qhzw.vip.qiyu.model.GuaranteeUserQuery;
import net.qhzw.vip.qiyu.result.JsonResult;
import net.qhzw.vip.qiyu.service.GuaranteeUserService;
import net.qhzw.vip.qiyu.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("sys/guarantee/user")
@Api(tags = "担保公司站点用户控制器")
public class SysGuaranteeUserController {

    @Autowired
    private GuaranteeUserService guaranteeUserService;

    @ApiOperation(value = "分页查询站点用户")
    @GetMapping
    public JsonResult findByPage(Page<GuaranteeUser> page, GuaranteeUserQuery query) {
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        Map<String, Object> result = guaranteeUserService.findByPage(page, shopId, query);
        return JsonResult.ok(result);
    }

    @ApiOperation(value = "根据ID查询站点用户")
    @GetMapping("{id}")
    public JsonResult<GuaranteeUser> findGuaranteeUser(@PathVariable Long id) {
        GuaranteeUser guaranteeUser = guaranteeUserService.getById(id);
        return JsonResult.ok(guaranteeUser);
    }

    @ApiOperation(value = "添加站点用户")
    @PostMapping
    public JsonResult addGuaranteeUser(@RequestBody GuaranteeUser guaranteeUser) {
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        int count = guaranteeUserService.count(new LambdaQueryWrapper<GuaranteeUser>()
                .eq(GuaranteeUser::getIdCard, guaranteeUser.getIdCard())
                .eq(GuaranteeUser::getStatus, 1));
        if (count > 0) {
            throw new MyException(guaranteeUser.getRealName() + " 已存在，不能重复添加! 请先禁用或删除后操作");
        }
        guaranteeUser.setId(IdWorker.getId());
        guaranteeUser.setShopId(shopId);
        guaranteeUser.setIdCard(guaranteeUser.getIdCard().toUpperCase());
        guaranteeUser.setStatus((byte) 1);
        guaranteeUser.setCreated(new Date());
        boolean save = guaranteeUserService.save(guaranteeUser);
        if (save) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    @ApiOperation(value = "修改站点用户")
    @PutMapping
    public JsonResult updateGuaranteeUser(@RequestBody GuaranteeUser guaranteeUser) {
        guaranteeUser.setIdCard(guaranteeUser.getIdCard().toUpperCase());
        boolean update = guaranteeUserService.updateById(guaranteeUser);
        if (update) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    @ApiOperation(value = "设置站点用户状态")
    @PutMapping("setStatus")
    public JsonResult setStatus(@RequestBody GuaranteeUser guaranteeUser) {
        boolean save = guaranteeUserService.updateById(guaranteeUser);
        //发送短信通知
        guaranteeUserService.sendMsg(guaranteeUser);
        if (save) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    @ApiOperation(value = "删除站点用户")
    @DeleteMapping("{id}")
    public JsonResult delGuaranteeUser(@PathVariable String id) {
        List<String> list = Arrays.asList(id.split(","));
        boolean remove = guaranteeUserService.removeByIds(list);
        if (remove) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    @ApiOperation(value = "查询站点用户下拉框")
    @GetMapping("selectBox/{status}")
    public JsonResult selectBox(@PathVariable Byte status) {
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        List<GuaranteeUser> list = guaranteeUserService.list(new LambdaQueryWrapper<GuaranteeUser>()
                .eq(GuaranteeUser::getStatus, status)
                .eq(shopId != 1, GuaranteeUser::getShopId, shopId));
        List<BoxVo> boxs = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            for (GuaranteeUser guaranteeUser : list) {
                boxs.add(new BoxVo(guaranteeUser.getId(), guaranteeUser.getRealName()));
            }
        }
        return JsonResult.ok(boxs);
    }


}
